package com.webserve.core;

import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

/**
 * 该线程任务负责指定的客户端HTTP交互
 */
public class ClientHandler implements Runnable{

    private Socket socket;
    public ClientHandler(Socket socket){
        this.socket=socket;
    }

    public void run(){
        try {
            InputStream in=socket.getInputStream();
            StringBuilder builder=new StringBuilder();
            int d;
            char pre='a',cur='a';//pre上次读到的字符 cur本次读到的字符
            while ((d=in.read())!=-1){
                cur=(char)d;//将本次读到的字节转为字符赋值给cur
                if(pre==13&cur==10){//判断是否读到了回车+换行
                    break;
                }
                builder.append(cur);//将本次读到的字符拼接到stringbuilder中
                pre=cur;//在进行下一个字符读取前将本次读取的字符记录为上次读到的字符
            }
            String line=builder.toString().trim();
            System.out.println(line);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
